package com.board.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.board.dao.AccountRoleMapper;
import com.board.dao.AdminInfoMapper;
import com.board.dao.RoleMapper;
import com.board.dto.AccountDto;
import com.board.dto.AccountNewDto;
import com.board.dto.AdminInfoDto;
import com.board.entity.AccountRole;
import com.board.entity.AdminInfo;
import com.board.entity.Role;
import com.board.exception.AdminInfoException;
import com.board.form.UpdateAdminPassForm;
import com.board.param.AddAdminAccountParam;
import com.board.param.AdminLoginParam;
import com.board.service.AdminInfoService;
import com.board.util.MD5Util;
import com.board.util.PageInfo;
import com.board.util.StateDate;
import jdk.nashorn.internal.runtime.regexp.joni.WarnCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@Service
public class AdminInfoServiceImpl extends ServiceImpl<AdminInfoMapper, AdminInfo> implements AdminInfoService {
    @Autowired
    private AdminInfoMapper adminInfoMapper;
    @Autowired
    private AccountRoleMapper accountRoleMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public AdminInfoDto findAdminById(Integer adminId) {
        //根据id查询管理员信息
        AdminInfo adminInfo = adminInfoMapper.selectById(adminId);
        if (adminInfo==null) throw new AdminInfoException(500,"管理员不存在");
        AdminInfoDto dto = BeanUtil.toBean(adminInfo, AdminInfoDto.class);
        return dto;
    }

    @Override
    public AccountNewDto addAdminAccount(AddAdminAccountParam param) {
        //账号名是否已存在
        QueryWrapper<AdminInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_user",param.getAdminUser());
        AdminInfo adminInfo = adminInfoMapper.selectOne(wrapper);
        if (adminInfo!=null) throw new AdminInfoException(500,"账号名已存在");
        //可以进行新增
        AdminInfo info = BeanUtil.toBean(param, AdminInfo.class);
        info.setAdminState(StateDate.ADMIN_STATE_NORMAL);
        //密码加密
        String password = MD5Util.md5(param.getAdminUser(), param.getAdminPass());
        info.setAdminPass(password);
        adminInfoMapper.insert(info);
        //添加账号角色表数据,新增的账号默认工作人员角色
        AccountRole accountRole = new AccountRole();
        accountRole.setAccountId(info.getAdminId());
        accountRole.setRoleId(2);
        accountRoleMapper.insert(accountRole);
        //返回数据
        AccountNewDto dto = new AccountNewDto();
        dto.setAdminId(info.getAdminId());
        dto.setAdminUser(info.getAdminUser());
        dto.setRoleName("工作人员");
        return dto;
    }

    @Override
    public AccountNewDto adminLogin(AdminLoginParam param) {
        //管理员登录,先判断用户名是否存在
        AdminInfo adminUser = adminInfoMapper.selectOne(new QueryWrapper<AdminInfo>().eq("admin_user", param.getAdminUser()));
        if (adminUser==null) throw new AdminInfoException(500,"账号或密码错误");
        //登录的账号状态要正常
        if (adminUser.getAdminState()!=StateDate.ADMIN_STATE_NORMAL){
            throw new AdminInfoException(500,"账号状态异常");
        }
        //判断密码是否正确
        String loginPass = MD5Util.md5(param.getAdminUser(), param.getAdminPass());
        if (!loginPass.equals(adminUser.getAdminPass())){
            throw new AdminInfoException(500,"账号或密码错误");
        }
        //可以登录，返回dto
        AccountNewDto dto = BeanUtil.toBean(adminUser, AccountNewDto.class);
        AccountRole accountRole = accountRoleMapper.selectOne(new QueryWrapper<AccountRole>().eq("account_id", adminUser.getAdminId()));
        Role role = roleMapper.selectById(accountRole.getRoleId());
        dto.setRoleName(role.getRoleName());
        return dto;
    }

    @Override
    public AccountDto disableAdmin(Integer adminId,Integer loginId) {
        //不能禁用自己的id
        if (adminId==loginId) throw new AdminInfoException(500,"不能禁用自己");
        //查询禁用的账号id是否存在
        AdminInfo adminInfo = adminInfoMapper.selectById(adminId);
        if (adminInfo==null) throw new AdminInfoException(500,"账号不存在");
        //已被删除的账号不能被禁用
        if (adminInfo.getAdminState()==StateDate.ADMIN_STATE_DELETE){
            throw new AdminInfoException(500,"账号已被删除");
        }
        //不能重复禁用
        if (adminInfo.getAdminState()==StateDate.ADMIN_STATE_DISABLE){
            throw new AdminInfoException(500,"不能重复禁用");
        }
        //可以禁用
        adminInfo.setAdminState(StateDate.ADMIN_STATE_DISABLE);
        adminInfoMapper.updateById(adminInfo);
        AccountDto dto = BeanUtil.toBean(adminInfo, AccountDto.class);
        dto.setStateName("禁用");
        return dto;
    }

    @Override
    public AccountDto enableAdmin(Integer adminId,Integer loginId) {
        //不能启用自己的账号
        if (adminId==loginId) throw new AdminInfoException(500,"不能启用自己");
        //查询启用的账号id是否存在
        AdminInfo adminInfo = adminInfoMapper.selectById(adminId);
        if (adminInfo==null) throw new AdminInfoException(500,"账号不存在");
        //已被删除的账号不能被启用
        if (adminInfo.getAdminState()==StateDate.ADMIN_STATE_DELETE){
            throw new AdminInfoException(500,"账号已被删除");
        }
        //不能重复启用
        if (adminInfo.getAdminState()==StateDate.ADMIN_STATE_NORMAL){
            throw new AdminInfoException(500,"不能重复启用");
        }
        //可以启用
        adminInfo.setAdminState(StateDate.ADMIN_STATE_NORMAL);
        adminInfoMapper.updateById(adminInfo);
        AccountDto dto = BeanUtil.toBean(adminInfo, AccountDto.class);
        dto.setStateName("启用");
        return dto;
    }

    @Override
    public void deleteAdmin(Integer adminId, Integer loginId) {
        //不能删除自己
        if (adminId==loginId) throw new AdminInfoException(500,"不能删除自己");
        //删除的账号是否存在
        AdminInfo adminInfo = adminInfoMapper.selectById(adminId);
        if (adminInfo==null) throw new AdminInfoException(500,"账号不存在");
        //已删除的账号不能重复删除
        if (adminInfo.getAdminState()==StateDate.ADMIN_STATE_DELETE){
            throw new AdminInfoException(500,"账号不能重复删除");
        }
        //可以进行删除
        adminInfo.setAdminState(StateDate.ADMIN_STATE_DELETE);
        adminInfoMapper.updateById(adminInfo);
    }

    @Override
    public AccountNewDto updateAdminPass(UpdateAdminPassForm param, Integer loginId) {
        //先验证旧密码是否正确
        AdminInfo adminInfo = adminInfoMapper.selectById(loginId);
        String oldPass = MD5Util.md5(adminInfo.getAdminUser(), param.getOldPass());
        if (!adminInfo.getAdminPass().equals(oldPass)){
            throw new AdminInfoException(500,"旧密码错误");
        }
        //可以进行修改密码
        String newPass = MD5Util.md5(adminInfo.getAdminUser(), param.getNewPass());
        adminInfo.setAdminPass(newPass);
        adminInfoMapper.updateById(adminInfo);
        AccountNewDto dto = BeanUtil.toBean(adminInfo, AccountNewDto.class);
        AccountRole accountRole = accountRoleMapper.selectOne(new QueryWrapper<AccountRole>().eq("account_id", dto.getAdminId()));
        Role role = roleMapper.selectById(accountRole.getRoleId());
        dto.setRoleName(role.getRoleName());;
        return dto;
    }

    @Override
    public PageInfo<AccountDto> findAllAdmin(String adminUser, Integer pageNum, Integer pageSize) {
        Page<AdminInfo> page = new Page<>(pageNum==null?1:pageNum,pageSize==null?10:pageSize);
        QueryWrapper<AdminInfo> wrapper = new QueryWrapper<>();
        if (adminUser!=null && adminUser!=""){
            wrapper.like("admin_user",adminUser);
        }
        adminInfoMapper.selectPage(page, wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        List<AccountDto> list = BeanUtil.copyToList(page.getRecords(), AccountDto.class);
        for (AccountDto dto : list) {
            dto.setStateName(dto.getAdminState()==0?"正常":dto.getAdminState()==1?"禁用":"删除");
        }
        pageInfo.setList(list);
        pageInfo.setPages(page.getPages());
        return pageInfo;
    }
}
